package com.tianfeng.huayi.controller;

import com.tianfeng.huayi.entity.Feeitem;
import com.tianfeng.huayi.entity.PaymentPropertyFeeitm;
import com.tianfeng.huayi.entity.PaymentRecord;
import com.tianfeng.huayi.entity.Property;
import com.tianfeng.huayi.service.IPaymentRecordService;
import com.tianfeng.huayi.service.IPropertyService;
import com.tianfeng.huayi.util.CurrentDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/paymentRecord")
@CrossOrigin
public class PaymentRecordController {

    @Autowired
    private IPaymentRecordService paymentRecordService;
    @Autowired
    private IPropertyService propertyService;

    /**
     查询缴费记录列表
     */
    @PostMapping("/list")
    public ResponseEntity<Map<Object, Object>> list(@RequestBody PaymentRecord paymentRecord) {
        Map<Object,Object> response = new HashMap<>();
        List<PaymentPropertyFeeitm> paymentPropertyFeeitms = new ArrayList<>();
        List<PaymentRecord> paymentRecords = paymentRecordService.selectPaymentRecordList(paymentRecord);
        if (!paymentRecords.isEmpty()) {
            for(PaymentRecord item: paymentRecords) {
                PaymentPropertyFeeitm paymentPropertyFeeitm = new PaymentPropertyFeeitm();
                Property property = propertyService.selectPropertyByPropertyId(item.getPropertyId());
                Feeitem feeitem = paymentRecordService.selectFeeitemByItemId(item.getItemId());
                paymentPropertyFeeitm.setPaymentRecord(item);
                paymentPropertyFeeitm.setProperty(property);
                paymentPropertyFeeitm.setFeeitem(feeitem);
                paymentPropertyFeeitms.add(paymentPropertyFeeitm);
            }
            response.put("code", 200);
            response.put("msg", "查询成功");
            response.put("data", paymentPropertyFeeitms);
        } else {
            response.put("code", 401);
            response.put("msg", "无缴费信息");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 插入缴费记录
     */
    @PostMapping("/insert")
    public ResponseEntity<Map<Object, Object>> insert(@RequestBody PaymentRecord paymentRecord) {
        Map<Object,Object> response = new HashMap<>();
        if (paymentRecord.getFlowingWater() == null || paymentRecord.getFlowingWater().isEmpty()) {
            // 生成UUID作为流水号
            String flowingWater = UUID.randomUUID().toString().replace("-", "");
            paymentRecord.setFlowingWater(flowingWater);
        }
        paymentRecord.setPaymentTime(CurrentDate.convertLocalDateTimeToDate(LocalDateTime.now()));
        int i = paymentRecordService.insertPaymentRecord(paymentRecord);
        if (i != 0) {
            response.put("code", 200);
            response.put("msg", "插入成功");
        } else {
            response.put("code", 400);
            response.put("msg", "插入失败");
        }
        return ResponseEntity.ok(response);
    }
}
